package com.eastfair.builder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.builder.dao.ReportBuilderMapper;
import com.eastfair.builder.dto.ReportBuilderDTO;
import com.eastfair.builder.entity.ReportBuilder;
import com.eastfair.builder.entity.ReportSpace;
import com.eastfair.builder.enumeration.ReportSpaceReportSpaceStatusEnum;
import com.eastfair.builder.exceptioncode.BuilderExceptionCode;
import com.eastfair.builder.service.ReportBuilderService;
import com.eastfair.builder.service.ReportSpaceService;
import com.eastfair.builder.vo.ReportBuilderVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.exception.NoBackBizException;
import com.eastfair.home.dto.ReportBuilderAuditDTO;
import com.eastfair.venueservice.consts.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆搭建商信息
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportBuilderServiceImpl extends SuperServiceImpl<ReportBuilderMapper, ReportBuilder> implements ReportBuilderService {

    @Resource
    private ReportSpaceService reportSpaceService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportBuilder> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportBuilder model) {
        if (getVOByReportSpaceId(model.getReportSpaceId()) != null) {
            throw NoBackBizException.wrap(BuilderExceptionCode.REPORT_BUILDER_HAS_EXIST);
        }
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public ReportBuilderVO getVOByReportSpaceId(Long reportSpaceId) {
        log.info("getReportBuilderVOByReportSpaceId - 报馆ID查询报馆搭建商信息, reportSpaceId={}", reportSpaceId);
        ReportBuilder reportBuilder = getByReportSpaceId(reportSpaceId);
        if (reportBuilder == null) {
            return null;
        }
        return BeanUtil.toBean(reportBuilder, ReportBuilderVO.class);
    }

    @Override
    public ReportBuilder getReportBuilder(ReportBuilderDTO query) {
        log.info("getReportBuilder - 获取报馆搭建商信息, query={}", query);
        QueryWrapper<ReportBuilder> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .last("limit 1")
                .eq(ReportBuilder::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportSpaceId() != null, ReportBuilder::getReportSpaceId, query.getReportSpaceId())
        ;
        return getOne(queryWrapper);
    }

    @Override
    public ReportBuilder getByReportSpaceId(Long reportSpaceId) {
        ReportBuilderDTO query = new ReportBuilderDTO();
        query.setReportSpaceId(reportSpaceId);
        return getReportBuilder(query);
    }

    @Override
    public ReportBuilderVO getVOByExhibitorId(Long exhibitorId) {
        log.info("getByExhibitorId - 展商ID查询报馆搭建商基本信息, exhibitorId={}", exhibitorId);
        ReportSpace reportSpace = reportSpaceService.getByExhibitorId(exhibitorId);
        if (reportSpace == null) {
            return null;
        }
        if (!ReportSpaceReportSpaceStatusEnum.APPROVED.eq(reportSpace.getReportSpaceStatus())) {
            return null;
        }
        return getVOByReportSpaceId(reportSpace.getId());
    }

    @Override
    public ReportBuilderAuditDTO createAuditByReportSpaceId(Long reportSpaceId) {
        ReportBuilderVO reportBuilder = getVOByReportSpaceId(reportSpaceId);
        if (reportBuilder == null) {
            throw BizException.wrap(BuilderExceptionCode.REPORT_BUILDER_NOT_EXIST.getCode(), "无法提交，请完善搭建商基本信息");
        }
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        return BeanUtil.toBean(reportBuilder, ReportBuilderAuditDTO.class, copyOptions);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByReportSpaceId(Long reportSpaceId) {
        ReportBuilder reportBuilder = getByReportSpaceId(reportSpaceId);
        if (reportBuilder == null) {
            return false;
        }
        List<ReportBuilder> list = new ArrayList<>();
        list.add(reportBuilder);
        return removeByIdsOfLogic(list);
    }
}
